//package com.usaas.search.filter;
//
//import cn.hutool.core.collection.CollUtil;
//import com.alibaba.fastjson.JSON;
//import com.usaas.common.constant.HttpStatus;
//import com.usaas.common.core.domain.AjaxResult;
//import com.usaas.common.utils.ServletUtils;
//import com.usaas.search.component.AppTokenComponent;
//import com.usaas.search.component.UserContextHolder;
//import com.usaas.search.dto.PlatUserDto;
//import com.usaas.search.exception.ExceptionUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.List;
//
//@Component
//@WebFilter(urlPatterns = {"/app/*"})
//public class AppApiFilter implements Filter {
//
//    @Autowired
//    private AppTokenComponent appTokenComponent;
//
//    public static final List<String> appWhiteList = CollUtil.newArrayList(
//            "/app/search/user/sendMobileLoginSms",
//            "/app/search/user/mobileLoginOrRegister"
//    );
//
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//        Filter.super.init(filterConfig);
//    }
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
//        try {
//            userParseAndSetUserInfo(httpServletRequest);
//            loginCheck(httpServletRequest, httpServletResponse);
//        } catch (Exception e) {
//            renderUnLogin(httpServletRequest, httpServletResponse);
//            return;
//        }
//        filterChain.doFilter(request, response);
//        clearUserInfo();
//    }
//
//    private PlatUserDto userParseAndSetUserInfo(HttpServletRequest httpServletRequest) {
//        PlatUserDto userInfo = appTokenComponent.getUserInfo(httpServletRequest);
//        if (userInfo != null) {
//            String token = appTokenComponent.getToken(httpServletRequest);
//            UserContextHolder.set(userInfo, token);
//        }
//        return userInfo;
//    }
//
//    private void clearUserInfo() {
//        UserContextHolder.clear();
//    }
//
//    //实名认证校验
//    private void loginCheck(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
//        //如果是白名单里的 不需要校验
//        if (appWhiteList.contains(httpServletRequest.getRequestURI())) {
//            return;
//        }
//        //不是白名单 校验是否登录
//        if (appTokenComponent.getUserInfo() == null) {
//            ExceptionUtil.throwError("登录失效,请重新登录");
//        }
//    }
//
//
//    private void renderUnLogin(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
//        AjaxResult result = AjaxResult.error(HttpStatus.UNAUTHORIZED, "请登录");
//        String rsponseJson = JSON.toJSONString(result);
//        ServletUtils.renderString(httpServletResponse, rsponseJson);
//
//    }
//
//    @Override
//    public void destroy() {
//        Filter.super.destroy();
//    }
//}